package com.zhang.payment8001.controller;

import com.netflix.appinfo.InstanceInfo;
import com.zhang.payment8001.domain.Payment;
import com.zhang.payment8001.mapper.PaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Alger
 * @date 2021/8/16  21:04
 */
@RestController
@RequestMapping("payment")
public class PaymentController {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("list")
    public List<Payment> list(){
        List<Payment> payments = paymentMapper.selectList(null);
        //仅仅为了测试负载均衡
        Payment payment = new Payment();
        payment.setId(8001l);
        payments.add(payment);
        return payments;
    }
    @GetMapping("discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (int i = 0; i < services.size(); i++) {
            System.out.println(services.get(i));;
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance serviceInstance:instances){
            System.out.println(serviceInstance.getUri());
        }

        return this.discoveryClient;

    }
}
